package com.zhentao.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhentao.model.common.app.pojo.ApUser;
import com.zhentao.model.common.buiness.dto.BuUserLoginDto;
import com.zhentao.model.common.buiness.pojo.BuUser;
import com.zhentao.model.common.dtos.ResponseResult;
import com.zhentao.model.common.enums.AppHttpCodeEnum;
import com.zhentao.service.BuUserService;
import com.zhentao.mapper.BuUserMapper;
import com.zhentao.utils.common.AppJwtUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
* @author 薄超轩
* @description 针对表【bu_user】的数据库操作Service实现
* @createDate 2024-09-14 11:28:41
*/
@Service
public class BuUserServiceImpl implements BuUserService{
    @Autowired
    private BuUserMapper buUserMapper;


    /**
     * 商家端：用户登入操作
     * @param dto
     * @return
     */
    @Override
    public ResponseResult buUserLogin(BuUserLoginDto dto) {
        if (dto==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        QueryWrapper<BuUser>wrapper=new QueryWrapper<>();
        wrapper.lambda().eq(BuUser::getPhone, dto.getPhone());
        BuUser buUser = buUserMapper.selectOne(wrapper);
        if (buUser==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        String password = DigestUtils.md5DigestAsHex((dto.getPassword()+buUser.getSalt()).getBytes());
        if (!password.equals(buUser.getPassword())){
            return ResponseResult.errorResult(AppHttpCodeEnum.LOGIN_PASSWORD_ERROR);
        }
        Map<String,Object>map=new HashMap<>();
        map.put("token", AppJwtUtil.getToken(buUser.getId().longValue()));

        map.put("userInfo", buUser);
        return ResponseResult.okResult(map);
    }

    @Override
    public ResponseResult userregist(BuUserLoginDto dto) {

        if(org.apache.commons.lang3.StringUtils.isBlank(dto.getPhone())|| StringUtils.isBlank(dto.getPassword())){
            return ResponseResult.errorResult(AppHttpCodeEnum.LOGIN_PASSWORD_ERROR,"用户名或密码不能为空");
        }else {
            QueryWrapper<BuUser> queryWrapper=new QueryWrapper<>();
            queryWrapper.lambda().eq(BuUser::getPhone,dto.getPhone());
            BuUser buUser = buUserMapper.selectOne(queryWrapper);
            System.out.println("手机号查询到的详细信息 = " + buUser);
            if(buUser != null){
                return ResponseResult.errorResult(AppHttpCodeEnum.LOGIN_PASSWORD_ERROR,"此用户已存在");
            }else {
                BuUser buUser1 = new BuUser();
                BeanUtils.copyProperties(dto,buUser1);

//                给密码加盐
                String Salt ="123";
                String pwd = buUser1.getPassword();
                pwd= DigestUtils.md5DigestAsHex((pwd+Salt).getBytes());
                buUser1.setPassword(pwd);
                buUser1.setSalt(Salt);
//                新增商家
                buUserMapper.insert(buUser1);
                return ResponseResult.okResult(buUser1);
            }
        }
    }


    /**
     * 商家端：根据id查询
     * @param id
     * @return
     */
    @Override
    public ResponseResult userById(Integer id) {
        if (id==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        BuUser buUser = buUserMapper.selectById(id);
        return ResponseResult.okResult(buUser);
    }

    /**
     * 商家端：修改商家信息
     * @param buUser
     * @return
     */
    @Override
    public ResponseResult updateUser(BuUser buUser) {
        if (buUser==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        buUserMapper.updateById(buUser);
        return ResponseResult.okResult("成功");
    }
}




